package com.jy.rhin.application.cda;

import com.jy.api.hospital.HospitalAPI;
import com.jy.api.hospital.dto.HospitalDTO;
import com.jy.api.misc.dto.DaiMaDto;
import com.jy.api.misc.dto.ZhiYuMXCXDto;
import com.jy.api.patient.dto.BingRenXXDto;
import com.jy.common.StringUtil;
import com.jy.framework.base.utils.Converter;
import com.jy.rhin.application.cda.enums.HealthDictionaryEnum;
import com.jy.rhin.domain.cda.CDACreator;
import com.jy.rhin.infra.cda.CDAException;
import com.jy.rhin.infra.cda.dao.GeRenJKXXDJDao;
import com.jy.rhin.infra.cda.feign.PatientService;
import com.jy.rhin.infra.cda.feign.ShuYuService;
import com.jy.rhin.infra.cda.gerenjbjkxx.*;
import com.jy.rhin.infra.cda.util.ZhiJiUtils;
import com.jy.rhin.model.ClinicalDocumentData;
import com.jy.rhin.model.CodeObject;
import com.jy.rhin.model.GeRenJBXXDJ;
import com.jy.rhin.model.body.canjishi.CanJiShi;
import com.jy.rhin.model.body.guominshi.GuoMinShi;
import com.jy.rhin.model.body.jiwangshi.*;
import com.jy.rhin.model.body.shenghuohuanjing.*;
import com.jy.rhin.model.body.shiyanshijiancha.ShiYanShiXX;
import com.jy.rhin.model.body.yichuanbingshi.YiChuanBS;
import com.jy.rhin.model.body.zhiyebaolushi.ZhiYeBLS;
import com.jy.rhin.model.header.*;
import com.jy.rhin.utils.CDAUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

@Service
public class GeRenJBJKXXApplication {

    private static final String FTL_PATH = "/01gerenjibenxinxi.ftl";

    @Resource
    private CDACreator cdaCreator;
   /* @Resource
    private DataMigrationAPI dataMigrationAPI;*/
    @Autowired
    private GeRenJKXXDJDao geRenJKXXDJDao;
    @Autowired
    private PatientService patientService;
    @Autowired
    private ShuYuService shuYuService;
    @Resource
    private HospitalAPI hospitalAPI;

    public String create(String gerendabs, String yiliaojgdm) {

        GeRenJBXXDJ docData = new GeRenJBXXDJ();

        GeRenJKDASYVo geRenJKDASYVo = geRenJKXXDJDao.getGeRenJKDASY(gerendabs, yiliaojgdm);
        if (Objects.isNull(geRenJKDASYVo)) {
            throw new CDAException(gerendabs + "个人健康档案首页数据不存在", null);
        }
        HospitalDTO hospital = hospitalAPI.info(yiliaojgdm).getData();
//        HospitalDTO hospital = dataMigrationAPI.getByHospitalId(yiliaojgdm).getData();
        if (Objects.isNull(hospital)) {
            throw new CDAException("机构数据不存在", null);
        }
        GeRenJBXXVo geRenJBXXVo = geRenJKXXDJDao.getGeRenJBXX(gerendabs, yiliaojgdm);
        if (Objects.isNull(geRenJBXXVo)) {
            throw new CDAException(gerendabs + "个人基本信息数据不存在", null);
        }
        BingRenXXDto bingrenxx = patientService.queryPatientById(geRenJKDASYVo.getPatientId());
        tranData(geRenJKDASYVo, geRenJBXXVo, hospital);

        shuYuZhiYuQuery(bingrenxx, geRenJKDASYVo, geRenJBXXVo);
        setClinicalDocumentData(docData, geRenJKDASYVo, hospital, geRenJBXXVo);

        docData.setId(geRenJKDASYVo.getAppId());
        docData.setParticipant(createParticipant(bingrenxx));
        docData.setShiYanShiXX(createShiYanShiXX(geRenJKDASYVo));
        docData.setYiLiaoFKFS(new CodeObject("99", "其他"));
        docData.setGuoMinShi(createGuoMinShi(bingrenxx));
        docData.setZhiYeBLS(createZhiYeBLS(geRenJKDASYVo));
        docData.setJiWangSJWJB(createJiWangSJWJB(geRenJKDASYVo));
        docData.setShouShuShi(createShouShuShi(geRenJKDASYVo));
        docData.setShuXueShi(createShuXueShi(geRenJKDASYVo));
        docData.setWaiShangShi(createWaiShangShi(geRenJKDASYVo));
        docData.setYiChuanBS(createYiChuanBS(geRenJKDASYVo));
        docData.setCanJiShi(createCanJiShi(geRenJKDASYVo));
        docData.setJiaZuShi(createJiaZuShi(geRenJKDASYVo));
        docData.setJiaTingCFPFSSLB(createJiaTingCFPFSSLB(geRenJKDASYVo));
        docData.setJiaTingCSLB(createJiaTingCSLB(geRenJKDASYVo));
        docData.setJiaTingQCLLB(createJiaTingQCLLB(geRenJKDASYVo));
        docData.setJiaTingRLLXLB(createJiaTingRLLXLB(geRenJKDASYVo));
        docData.setJiaTingYSLB(createJiaTingYSLB(geRenJKDASYVo));

        setDefaultDocData(docData);
        return cdaCreator.create(docData, FTL_PATH);
    }

    private List<JiaZuShi> createJiaZuShi(GeRenJKDASYVo vo) {
        List<JiaZuShi> list = new ArrayList<>();
        List<GeRenJZSVo> jiaZuS = geRenJKXXDJDao.getJiaZuS(vo.getGERENDABS(), vo.getYEWUJGDM());
        if (jiaZuS == null || jiaZuS.size() == 0) {
            JiaZuShi yiChuanBS = new JiaZuShi();
            yiChuanBS.setJiaTingGX(new CodeObject("01", "本人"));
            yiChuanBS.setJiaZuXJB(new CodeObject("02", "高血压"));
            list.add(yiChuanBS);
        }
        for (GeRenJZSVo ycs : jiaZuS) {
            JiaZuShi yiChuanBS = new JiaZuShi();
            yiChuanBS.setJiaTingGX(new CodeObject(ycs.getYUBENRGX(), getGuanXiMC(ycs.getYUBENRGX())));
            yiChuanBS.setJiaZuXJB(new CodeObject(ycs.getJIBINGDM(), getGuanXiMC(ycs.getJIBINGMC())));
            list.add(yiChuanBS);
        }
        return list;
    }

    private String getGuanXiMC(String yubenrgx) {
        String guanXiMC = "";
        switch (yubenrgx) {
            case "01":
                guanXiMC = "本人";
                break;
            case "02":
                guanXiMC = "户主";
                break;
            case "52":
                guanXiMC = "母亲";
                break;
            case "51":
                guanXiMC = "父亲";
                break;
        }
        return guanXiMC;
    }

    private void tranData(GeRenJKDASYVo geRenJKDASYVo, GeRenJBXXVo geRenJBXXVo, HospitalDTO hospital) {
        geRenJBXXVo.setGERENDABS(toLength17(geRenJBXXVo.getGERENDABS()));

        geRenJKDASYVo.setGERENDABS(toLength17(geRenJKDASYVo.getGERENDABS()));

//        hospital.setHospitalId(to10YiLiaoJGDM(hospital.getHospitalId()));
    }

    private HashSet<String> createHashSet(String... args) {
        HashSet<String> set = new HashSet<>();
        for (String s : args) {
            if (StringUtil.isNullOrEmpty(s)) continue;
            set.add(s);
        }
        return set;
    }

    private void shuYuZhiYuQuery(BingRenXXDto bingrenxx, GeRenJKDASYVo geRenJKDASYVo, GeRenJBXXVo geRenJBXXVo) {
        List<ZhiYuMXCXDto> zhiJiBSFList = new ArrayList<>();
        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.GB_T_2261_1.getCode(), new HashSet<>(createHashSet(geRenJBXXVo.getXINGBIEDM())));
        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.GBT_2261_2.getCode(), new HashSet<>(createHashSet(geRenJBXXVo.getHUNYINZKDM())));
        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.GBT_3304.getCode(), new HashSet<>(createHashSet(geRenJBXXVo.getMINZUDM())));
        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.GBT_4658.getCode(), new HashSet<>(createHashSet(geRenJBXXVo.getWENHUACDDM())));
        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.GBT_6565.getCode(), new HashSet<>(createHashSet(geRenJBXXVo.getZHIYEDM())));
        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV04_50_005.getCode(), new HashSet<>(createHashSet(geRenJKDASYVo.getABOXXDM())));
        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV04_50_020.getCode(), new HashSet<>(createHashSet(geRenJKDASYVo.getRHXXDM())));
        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV03_00_115.getCode(), new HashSet<>(createHashSet(geRenJKDASYVo.getYINSHUILBDM())));
        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV03_00_303.getCode(), new HashSet<>(createHashSet(geRenJKDASYVo.getRANLIAOLBDM())));
        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_QCL.getCode(), new HashSet<>(createHashSet(geRenJKDASYVo.getQINCHULLBDM())));
        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV03_00_304.getCode(), new HashSet<>(createHashSet(geRenJKDASYVo.getWEISHENGCSLBDM())));
        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV03_00_302.getCode(), new HashSet<>(createHashSet(geRenJKDASYVo.getCHUFANGPFSSDM())));
        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV05_10_001.getCode(), new HashSet<>(createHashSet(geRenJKDASYVo.getCANJIQKDM())));
        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV03_00_301.getCode(), new HashSet<>(createHashSet(geRenJKDASYVo.getBAOLUSHI())));

        Map<String, List<DaiMaDto>> valueByCode = shuYuService.getValueByCode(zhiJiBSFList.toArray(new ZhiYuMXCXDto[0]));

        String XINGBIEMC = ZhiJiUtils.getValue(HealthDictionaryEnum.GB_T_2261_1.getCode(), geRenJBXXVo.getXINGBIEDM(), valueByCode);
        if (!StringUtil.isNullOrEmpty(XINGBIEMC) && !XINGBIEMC.equals(geRenJBXXVo.getXINGBIEDM())) {
            geRenJBXXVo.setXINGBIEMC(XINGBIEMC);
        }

        String HUNYINZKMC = ZhiJiUtils.getValue(HealthDictionaryEnum.GBT_2261_2.getCode(), geRenJBXXVo.getHUNYINZKDM(), valueByCode);
        if (!StringUtil.isNullOrEmpty(HUNYINZKMC) && !HUNYINZKMC.equals(geRenJBXXVo.getHUNYINZKDM())) {
            geRenJBXXVo.setHUNYINZKMC(HUNYINZKMC);
        }

        String minzu = ZhiJiUtils.getValue(HealthDictionaryEnum.GBT_3304.getCode(), geRenJBXXVo.getMINZUDM(), valueByCode);
        if (!StringUtil.isNullOrEmpty(minzu) && !minzu.equals(geRenJBXXVo.getMINZUDM())) {
            geRenJBXXVo.setMINZUMC(minzu);
        }

        String wenhua = ZhiJiUtils.getValue(HealthDictionaryEnum.GBT_4658.getCode(), geRenJBXXVo.getWENHUACDDM(), valueByCode);
        if (!StringUtil.isNullOrEmpty(wenhua) && !wenhua.equals(geRenJBXXVo.getWENHUACDDM())) {
            geRenJBXXVo.setWENHUACDMC(wenhua);
        }

        String zhiye = ZhiJiUtils.getValue(HealthDictionaryEnum.GBT_6565.getCode(), geRenJBXXVo.getZHIYEDM(), valueByCode);
        if (!StringUtil.isNullOrEmpty(zhiye) && !zhiye.equals(geRenJBXXVo.getZHIYEDM())) {
            geRenJBXXVo.setZHIYEMC(zhiye);
        }

        String abo = ZhiJiUtils.getValue(HealthDictionaryEnum.CV04_50_005.getCode(), geRenJKDASYVo.getABOXXDM(), valueByCode);
        if (!StringUtil.isNullOrEmpty(abo) && !abo.equals(geRenJKDASYVo.getABOXXDM())) {
            geRenJKDASYVo.setABOXXMC(abo);
        }

        String rh = ZhiJiUtils.getValue(HealthDictionaryEnum.CV04_50_020.getCode(), geRenJKDASYVo.getRHXXDM(), valueByCode);
        if (!StringUtil.isNullOrEmpty(rh) && !rh.equals(geRenJBXXVo.getRHXXDM())) {
            geRenJKDASYVo.setRHXXMC(rh);
        }

        String yinshui = ZhiJiUtils.getValue(HealthDictionaryEnum.CV03_00_115.getCode(), geRenJKDASYVo.getYINSHUILBDM(), valueByCode);
        if (!StringUtil.isNullOrEmpty(yinshui) && !yinshui.equals(geRenJKDASYVo.getYINSHUILBDM())) {
            geRenJKDASYVo.setYINSHUILBMC(yinshui);
        }

        String ranliao = ZhiJiUtils.getValue(HealthDictionaryEnum.CV03_00_303.getCode(), geRenJKDASYVo.getRANLIAOLBDM(), valueByCode);
        if (!StringUtil.isNullOrEmpty(ranliao) && !ranliao.equals(geRenJKDASYVo.getRANLIAOLBDM())) {
            geRenJKDASYVo.setRANLIAOLBMC(ranliao);
        }

        String qinchul = ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_QCL.getCode(), geRenJKDASYVo.getQINCHULLBDM(), valueByCode);
        if (!StringUtil.isNullOrEmpty(qinchul) && !qinchul.equals(geRenJKDASYVo.getQINCHULLBDM())) {
            geRenJKDASYVo.setQINCHULLBMC(qinchul);
        }

        String weishengcs = ZhiJiUtils.getValue(HealthDictionaryEnum.CV03_00_304.getCode(), geRenJKDASYVo.getWEISHENGCSLBDM(), valueByCode);
        if (!StringUtil.isNullOrEmpty(weishengcs) && !weishengcs.equals(geRenJKDASYVo.getWEISHENGCSLBDM())) {
            geRenJKDASYVo.setWEISHENGCSLBMC(weishengcs);
        }

        String chufangpf = ZhiJiUtils.getValue(HealthDictionaryEnum.CV03_00_302.getCode(), geRenJKDASYVo.getCHUFANGPFSSDM(), valueByCode);
        if (!StringUtil.isNullOrEmpty(chufangpf) && !chufangpf.equals(geRenJKDASYVo.getCHUFANGPFSSDM())) {
            geRenJKDASYVo.setCHUFANGPFSSMC(chufangpf);
        }

        String canjiqk = ZhiJiUtils.getValue(HealthDictionaryEnum.CV05_10_001.getCode(), geRenJKDASYVo.getCANJIQKDM(), valueByCode);
        if (!StringUtil.isNullOrEmpty(canjiqk) && !canjiqk.equals(geRenJKDASYVo.getCANJIQKDM())) {
            geRenJKDASYVo.setCANJIQKMC(canjiqk);
        }
    }

    private void setClinicalDocumentData(ClinicalDocumentData clinicalDocumentData,
                                         GeRenJKDASYVo gerenjkda,
                                         HospitalDTO hospital,
                                         GeRenJBXXVo geRenJBXX) {
        clinicalDocumentData.setEffectiveTime(toDT15(gerenjkda.getGENGXINSJ()));
        clinicalDocumentData.setPatientRole(createPatientRole(gerenjkda, geRenJBXX));
        clinicalDocumentData.setAuthor(createAuthor(gerenjkda, hospital));
        clinicalDocumentData.setCustodian(createCustodian(hospital));
        clinicalDocumentData.setSetId(String.valueOf(gerenjkda.getGERENDABS()));
        clinicalDocumentData.setVersionNumber("1");
    }

    Custodian createCustodian(HospitalDTO hospital) {
        Custodian custodian = new Custodian();
        custodian.setId(hospital.getOutId());
        custodian.setName(hospital.getName());
        custodian.setTelecom(hospital.getDanWeiDH());
        custodian.setAddr(hospital.getAddress());
        return custodian;
    }

    Author createAuthor(GeRenJKDASYVo info, HospitalDTO hospital) {
        Author author = new Author();
        author.setId(info.getJIANDANGYSBM());
        author.setTime(toD8(info.getJIANDANGRQ()) + "111000");
        author.setName(info.getJIANDANGYSXM());
        author.setOrganizationId(hospital.getOutId());
        author.setOrganizationName(hospital.getName());
        author.setOrganizationAddr(hospital.getAddress());
        return author;
    }

    PatientRole createPatientRole(GeRenJKDASYVo gerenjkda, GeRenJBXXVo geRenJBXX) {
        PatientRole patientRole = new PatientRole();

        patientRole.setId(CDAUtil.formatNumber(gerenjkda.getGERENDABS()));
        patientRole.setPatient(createPatient(gerenjkda, geRenJBXX));
        patientRole.setTelecom(geRenJBXX.getSHOUJIHM());

        Addr addr = new Addr();
        addr.setStreetName(geRenJBXX.getJUZHUXXDZ());
        addr.setTownship(geRenJBXX.getJUZHUDZXZMC());
        addr.setCounty(geRenJBXX.getJUZHUDZXMC());
        addr.setCity(geRenJBXX.getJUZHUDZSMC());
        addr.setState(geRenJBXX.getJUZHUDZSFMC());
        patientRole.setAddr(addr);
        return patientRole;
    }

    Patient createPatient(GeRenJKDASYVo gerenjkda, GeRenJBXXVo geRenJBXX) {
        Patient patient = new Patient();
        patient.setName(gerenjkda.getXINGMING());
        patient.setId(gerenjkda.getZHENGJIANHM());
        patient.setAdministrativeGender(new CodeObject(geRenJBXX.getXINGBIEDM(), geRenJBXX.getXINGBIEMC()));
        patient.setMaritalStatus(new CodeObject(geRenJBXX.getHUNYINZKDM(), geRenJBXX.getHUNYINZKMC()));
        patient.setEthnicGroup(new CodeObject(geRenJBXX.getMINZUDM(), geRenJBXX.getMINZUMC()));
//        patient.setEmployerOrganization(gerenjkda.getGONGZUODWMC());
        patient.setHouseType(String.valueOf(!StringUtil.isNullOrEmpty(geRenJBXX.getCHANGZHULX())).toLowerCase());
        patient.setEducationLevel(new CodeObject(geRenJBXX.getWENHUACDDM(), geRenJBXX.getWENHUACDMC()));
        patient.setOccupation(new CodeObject(geRenJBXX.getZHIYEDM(), geRenJBXX.getZHIYEMC()));
        patient.setBirthTime(getBirthday(gerenjkda.getZHENGJIANHM()));
        patient.setEmployerOrganization("广东省深圳市罗湖区人民南路与嘉宾路");
//        Addr addr = new Addr();
////        addr.setHouseNumber();
//        addr.setStreetName(geRenJBXX.getJUZHUXXDZ());
//        addr.setTownship(geRenJBXX.getJUZHUDZXZMC());
//        addr.setCounty(geRenJBXX.getJUZHUDZXMC());
//        addr.setCity(geRenJBXX.getJUZHUDZSMC());
//        addr.setState(geRenJBXX.getJUZHUDZSFMC());
//        patient.setHouseholdAddr(addr);
        return patient;
    }


    Participant createParticipant(BingRenXXDto bingrenxx) {
        Participant participant = new Participant();
        if (bingrenxx == null) return null;
        participant.setName(bingrenxx.getLianXiRenXM());
        participant.setTelecom(bingrenxx.getLianXiRenSJ());
        if(StringUtil.isNullOrEmpty(participant.getTelecom())){
            participant.setTelecom("15925660247");
        }
        return participant;
    }

    ShiYanShiXX createShiYanShiXX(GeRenJKDASYVo vo) {
        ShiYanShiXX shiYanShiXX = new ShiYanShiXX();
        if (vo.getABOXXDM() == null || vo.getRHXXDM() == null) {
            return null;
        }
        shiYanShiXX.setXueXingABO(new CodeObject(vo.getABOXXDM(), vo.getABOXXMC()));
        shiYanShiXX.setXueXingRH(new CodeObject(vo.getRHXXDM(), vo.getRHXXMC()));
        return shiYanShiXX;
    }

    List<GuoMinShi> createGuoMinShi(BingRenXXDto brxx) {
        List<GuoMinShiVo> voList = geRenJKXXDJDao.getGuoMinJL(String.valueOf(brxx.getEmpi()));
        List<GuoMinShi> guoMinShiList = new ArrayList<>();
        if (voList == null || voList.isEmpty()) {
            GuoMinShi guoMinShi = new GuoMinShi();
            guoMinShi.setYaoWuGMSBZ(true);
            guoMinShi.setGuoMinFSSJ("20231011");
            guoMinShi.setGuoMinYuan(new CodeObject("101", "青霉素类抗生素"));
            guoMinShiList.add(guoMinShi);
            return guoMinShiList;
        }
        for (GuoMinShiVo vo : voList) {
            GuoMinShi guoMinShi = new GuoMinShi();
            guoMinShi.setYaoWuGMSBZ(true);
            guoMinShi.setGuoMinFSSJ(toD8(vo.getFASHENGRQ()));
            guoMinShi.setGuoMinYuan(new CodeObject(vo.getGUOMINYDM(), vo.getGUOMINYMC()));
            guoMinShiList.add(guoMinShi);
        }
        return guoMinShiList;
    }

    ZhiYeBLS createZhiYeBLS(GeRenJKDASYVo vo) {
        if (StringUtil.isNullOrEmpty(vo.getBAOLUSHI())) {
            return null;
        }
        ZhiYuMXCXDto[] arr = new ZhiYuMXCXDto[1];
        ZhiYuMXCXDto zhiYu = new ZhiYuMXCXDto();
        zhiYu.setZhiJiBSF(HealthDictionaryEnum.CV03_00_301.getCode());
        List<String> codeList = new ArrayList<>();
        codeList.add(vo.getBAOLUSHI());
        zhiYu.setCodeList(codeList);
        arr[0] = zhiYu;

        Map<String, List<DaiMaDto>> map = shuYuService.getValueByCode(arr);
        List<DaiMaDto> daiMaList = map.get(HealthDictionaryEnum.CV03_00_301.getCode());
        DaiMaDto daima = daiMaList.stream().filter(p -> vo.getBAOLUSHI().equals(p.getCode())).findFirst().orElse(null);
        if (daima == null) return null;
        ZhiYeBLS zhiYeBLS = new ZhiYeBLS();
        zhiYeBLS.setZhiYeBLS(new CodeObject(daima.getCode(), daima.getValue()));
        return zhiYeBLS;
    }

    List<JiWangSJWJB> createJiWangSJWJB(GeRenJKDASYVo vo) {
        List<JiWangSJWJB> list = new ArrayList<>();
        List<GeRenJWSVo> geRenJWSVoList = geRenJKXXDJDao.getJiWangShi(vo.getGERENDABS(), vo.getYEWUJGDM());
        if (geRenJWSVoList == null || geRenJWSVoList.isEmpty()) {
            JiWangSJWJB jiWangSJWJB1 = new JiWangSJWJB();
//            jiWangSJWJB1.setJiBingZLMC("疾病1");
//            jiWangSJWJB1.setJiBingZLDM("0101");
            jiWangSJWJB1.setQueZhenRQ("20231011");
            jiWangSJWJB1.setJiBingZL(new CodeObject("03", "糖尿病"));
            list.add(jiWangSJWJB1);
            return list;
        }

        for (GeRenJWSVo jws : geRenJWSVoList) {
            JiWangSJWJB jiWangSJWJB1 = new JiWangSJWJB();
//            jiWangSJWJB1.setJiBingZLMC("疾病1");
//            jiWangSJWJB1.setJiBingZLDM("0101");
            jiWangSJWJB1.setQueZhenRQ(toD8(jws.getLURURQ()));
            jiWangSJWJB1.setJiBingZL(new CodeObject("03", "糖尿病"));
            list.add(jiWangSJWJB1);
        }

        return list;
    }

    List<ShouShuShi> createShouShuShi(GeRenJKDASYVo vo) {
        List<ShouShuShi> list = new ArrayList<>();
        List<GeRenSSSVo> geRenSSSVoList = geRenJKXXDJDao.getShouShuShi(vo.getGERENDABS(), vo.getYEWUJGDM());
        if (geRenSSSVoList == null || geRenSSSVoList.isEmpty()) {
            ShouShuShi shouShuShi1 = new ShouShuShi();
            shouShuShi1.setShouShuBZ("true");
            shouShuShi1.setShouShuSMS("手术");
            shouShuShi1.setShouShuRQ("20231011113000");
            list.add(shouShuShi1);
        }
        for (GeRenSSSVo sss : geRenSSSVoList) {
            ShouShuShi shouShuShi1 = new ShouShuShi();
            shouShuShi1.setShouShuBZ("true");
            shouShuShi1.setShouShuSMS(sss.getSHOUSHUMC());
            shouShuShi1.setShouShuRQ(toDT15(sss.getSHOUSHURQ()));
            list.add(shouShuShi1);
        }
        return list;
    }

    List<ShuXueShi> createShuXueShi(GeRenJKDASYVo vo) {
        List<ShuXueShi> list = new ArrayList<>();
        List<GeRenSXSVo> geRenSXSVoList = geRenJKXXDJDao.getShuXueShi(vo.getGERENDABS(), vo.getYEWUJGDM());
        if (geRenSXSVoList == null || geRenSXSVoList.size() == 0) {
            ShuXueShi shuXueShi = new ShuXueShi();
            shuXueShi.setShuXueBZ("true");
            shuXueShi.setShuXueRQ("20231011123010");
            shuXueShi.setShuXueYYMS("需要输血");
            list.add(shuXueShi);
        }
        for (GeRenSXSVo sxs : geRenSXSVoList) {
            ShuXueShi shuXueShi = new ShuXueShi();
            shuXueShi.setShuXueBZ("true");
            shuXueShi.setShuXueRQ(toD8(sxs.getSHUXUERQ()));
            shuXueShi.setShuXueYYMS(sxs.getSHUXUEYY());
            list.add(shuXueShi);
        }
        return list;
    }

    List<WaiShangShi> createWaiShangShi(GeRenJKDASYVo vo) {
        List<WaiShangShi> list = new ArrayList<>();
        List<GeRenWSSVo> geRenWSSVoList = geRenJKXXDJDao.getWaiShangShi(vo.getGERENDABS(), vo.getYEWUJGDM());
        if (geRenWSSVoList == null || geRenWSSVoList.size() == 0) {
            WaiShangShi waiShangShi = new WaiShangShi();
            waiShangShi.setWaiShangBZ("true");
            waiShangShi.setWaiShangRQ("20231011123054");
            waiShangShi.setWaiShangSMS("皮肤破损");
            list.add(waiShangShi);
        }
        for (GeRenWSSVo wss : geRenWSSVoList) {
            WaiShangShi waiShangShi = new WaiShangShi();
            waiShangShi.setWaiShangBZ("true");
            waiShangShi.setWaiShangRQ(toD8(wss.getWAISHANGRQ()));
            waiShangShi.setWaiShangSMS(wss.getWAISHANGMC());
            list.add(waiShangShi);
        }
        return list;
    }

    List<YiChuanBS> createYiChuanBS(GeRenJKDASYVo vo) {
        List<YiChuanBS> list = new ArrayList<>();
        List<GeRenYCSVo> geRenYCSVoList = geRenJKXXDJDao.getYiChuangShi(vo.getGERENDABS(), vo.getYEWUJGDM());
        if (geRenYCSVoList == null || geRenYCSVoList.size() == 0) {
            YiChuanBS yiChuanBS = new YiChuanBS();
            yiChuanBS.setYiChuanBMC("心脏病");
            list.add(yiChuanBS);
        }
        for (GeRenYCSVo ycs : geRenYCSVoList) {
            YiChuanBS yiChuanBS = new YiChuanBS();
            yiChuanBS.setYiChuanBMC(ycs.getJIBINGMC());
            list.add(yiChuanBS);
        }
        return list;
    }

    List<CanJiShi> createCanJiShi(GeRenJKDASYVo vo) {
       /* if (StringUtil.isNullOrEmpty(vo.getCANJIQKDM())
                || "01".equals(vo.getCANJIQKDM()))
            return null;*/
        List<CanJiShi> list = new ArrayList<>();
        if (StringUtil.isEmpty(vo.getCANJIQKDM())) {
            return list;
        }
        CanJiShi canJiShi1 = new CanJiShi();
        canJiShi1.setCanJiSJ(toD8(vo.getJIANDANGRQ()));
        canJiShi1.setCanJiQK(new CodeObject(vo.getCANJIQKDM(), vo.getCANJIQKMC()));
//        canJiShi1.setCanJiQKDM(vo.getCANJIQKDM());
//        canJiShi1.setCanJiQKMC(vo.getCANJIQKMC());
        list.add(canJiShi1);
        return list;
    }

    JiaTingCFPFSSLB createJiaTingCFPFSSLB(GeRenJKDASYVo vo) {
        if (StringUtil.isNullOrEmpty(vo.getCHUFANGPFSSDM())) {
            return null;
        }

        JiaTingCFPFSSLB res = new JiaTingCFPFSSLB();
        res.setChuFangPFSSBZ(true);
        res.setChuFangPFSSLB(new CodeObject(vo.getCHUFANGPFSSDM(), vo.getCHUFANGPFSSMC()));
//        res.setChuFangPFSSLBDM(vo.getCHUFANGPFSSDM());
//        res.setChuFangPFSSLBMC(vo.getCHUFANGPFSSMC());
        return res;
    }

    JiaTingCSLB createJiaTingCSLB(GeRenJKDASYVo vo) {
        if (StringUtil.isNullOrEmpty(vo.getWEISHENGCSLBDM())) {
            return null;
        }
        JiaTingCSLB res = new JiaTingCSLB();
        res.setCeSuo(new CodeObject(vo.getWEISHENGCSLBDM(), vo.getWEISHENGCSLBMC()));
//        res.setCeSuoLBDM(vo.getWEISHENGCSLBDM());
//        res.setCeSuoLBMC(vo.getWEISHENGCSLBMC());
        return res;
    }

    JiaTingQCLLB createJiaTingQCLLB(GeRenJKDASYVo vo) {
        if (StringUtil.isNullOrEmpty(vo.getQINCHULLBDM())) {
            return null;
        }

        JiaTingQCLLB res = new JiaTingQCLLB();
        res.setQinChuLan(new CodeObject(vo.getQINCHULLBDM(), vo.getQINCHULLBMC()));
//        res.setQinChuLLBDM(vo.getQINCHULLBDM());
//        res.setQinChuLLBMC(vo.getQINCHULLBMC());

        return res;
    }

    JiaTingRLLXLB createJiaTingRLLXLB(GeRenJKDASYVo vo) {
        if (StringUtil.isNullOrEmpty(vo.getRANLIAOLBDM())) {
            return null;
        }

        JiaTingRLLXLB res = new JiaTingRLLXLB();

        res.setRanLiaoLX(new CodeObject(vo.getRANLIAOLBDM(), vo.getRANLIAOLBMC()));
//        res.setRanLiaoLXLBDM("9");
//        res.setRanLiaoLXLBMC("其他");

        return res;
    }

    JiaTingYSLB createJiaTingYSLB(GeRenJKDASYVo vo) {
        if (StringUtil.isNullOrEmpty(vo.getYINSHUILBDM())) {
            return null;
        }

        JiaTingYSLB res = new JiaTingYSLB();
        res.setYinShui(new CodeObject(vo.getYINSHUILBDM(), vo.getYINSHUILBMC()));
//        res.setYinShuiLBDM(vo.getYINSHUILBDM());
//        res.setYinShuiLBMC(vo.getYINSHUILBMC());
        return res;
    }

    private String toD8(LocalDateTime date) {
        if (date == null) return null;
        return Converter.toString(date, "yyyyMMdd");
    }

    private String toDT15(LocalDateTime date) {
        if (date == null) return null;
        return Converter.toString(date, "yyyyMMddHHmmss");
    }

    private String getBirthday(String IDCard) {
        String birthday = "";
        String year = "";
        String month = "";
        String day = "";
        if (!StringUtil.isEmpty(IDCard)) {
            if (IDCard.length() == 15) {
                year = "19" + IDCard.substring(6, 8);
                month = IDCard.substring(8, 10);
                day = IDCard.substring(10, 12);
            } else if (IDCard.length() == 18) {
                year = IDCard.substring(6).substring(0, 4);
                month = IDCard.substring(10).substring(0, 2);
                day = IDCard.substring(12).substring(0, 2);
            }

            birthday = year + month + day;
        }
        return birthday;
    }

    private String toLength17(String gerendabs) {
        if (gerendabs.length() < 17) {
            return StringUtil.padRight(gerendabs, 17, '0');
        }
        return gerendabs.substring(0, 17);
    }

    private String to10YiLiaoJGDM(String yiliaojgdm) {
        return yiliaojgdm.substring(0, 10);
    }

    private void setDefaultDocData(GeRenJBXXDJ docdata) {
        if (docdata.getParticipant() == null || StringUtil.isEmpty(docdata.getParticipant().getName())) {
            Participant participant = new Participant();
            participant.setName("-");
            participant.setTelecom(CDAUtil.defaultMobile());
            participant.setBirthTime(CDAUtil.defaultD8());
            participant.setScopingOrgName("-");
            docdata.setParticipant(participant);
        }
        if (docdata.getShiYanShiXX() == null) {
            ShiYanShiXX shiYanShiXX = new ShiYanShiXX();
            shiYanShiXX.setXueXingABO(new CodeObject("1", "A型"));
            shiYanShiXX.setXueXingRH(new CodeObject("3", "不详"));
            docdata.setShiYanShiXX(shiYanShiXX);
        }
        if (docdata.getYiLiaoFKFS() == null) {
            docdata.setYiLiaoFKFS(new CodeObject("99", "其他"));
        }
        if (CollectionUtils.isEmpty(docdata.getGuoMinShi())) {
            GuoMinShi guoMinShi = new GuoMinShi();
            guoMinShi.setYaoWuGMSBZ(true);
            guoMinShi.setGuoMinFSSJ("20231011");
            guoMinShi.setGuoMinYuan(new CodeObject("101", "青霉素类抗生素"));
            docdata.setGuoMinShi(new ArrayList<>());
            docdata.getGuoMinShi().add(guoMinShi);
        }
        if (docdata.getZhiYeBLS() == null) {
            ZhiYeBLS zhiYeBLS = new ZhiYeBLS();
            zhiYeBLS.setZhiYeBLS(new CodeObject("1", "无"));
            docdata.setZhiYeBLS(zhiYeBLS);
        }
        if (CollectionUtils.isEmpty(docdata.getJiWangSJWJB())) {
            docdata.setJiWangSJWJB(new ArrayList<>());
            JiWangSJWJB item = new JiWangSJWJB();
            item.setQueZhenRQ("20231011");
            item.setJiBingZL(new CodeObject("02", "高血压"));
            docdata.getJiWangSJWJB().add(item);
        }
        if (CollectionUtils.isEmpty(docdata.getShouShuShi())) {
            docdata.setShouShuShi(new ArrayList<>());
            ShouShuShi item = new ShouShuShi();
            item.setShouShuBZ("false");
            item.setShouShuRQ(CDAUtil.defaultD8());
            item.setShouShuSMS("无");
            docdata.getShouShuShi().add(item);
        }
        if (CollectionUtils.isEmpty(docdata.getShuXueShi())) {
            docdata.setShuXueShi(new ArrayList<>());
            ShuXueShi item = new ShuXueShi();
            item.setShuXueBZ("false");
            item.setShuXueRQ(CDAUtil.defaultD8());
            item.setShuXueYYMS("无");
            docdata.getShuXueShi().add(item);
        }
        if (CollectionUtils.isEmpty(docdata.getWaiShangShi())) {
            docdata.setWaiShangShi(new ArrayList<>());
            WaiShangShi item = new WaiShangShi();
            item.setWaiShangBZ("false");
            item.setWaiShangRQ(CDAUtil.defaultD8());
            item.setWaiShangSMS("无");
            docdata.getWaiShangShi().add(item);
        }
        if (CollectionUtils.isEmpty(docdata.getJiaZuShi())) {
            docdata.setJiaZuShi(new ArrayList<>());
            JiaZuShi item = new JiaZuShi();
            item.setJiaTingGX(new CodeObject("01", "本人"));
            item.setJiaZuXJB(new CodeObject("02", "高血压"));
            docdata.getJiaZuShi().add(item);
        }
        if (CollectionUtils.isEmpty(docdata.getYiChuanBS())) {
            docdata.setYiChuanBS(new ArrayList<>());
            YiChuanBS item = new YiChuanBS();
            item.setYiChuanBMC("心脏病");
            docdata.getYiChuanBS().add(item);
        }
        if (CollectionUtils.isEmpty(docdata.getCanJiShi())) {
            docdata.setCanJiShi(new ArrayList<>());
            CanJiShi item = new CanJiShi();
            item.setCanJiSJ(CDAUtil.defaultD8());
            item.setCanJiQK(new CodeObject("01", "无残疾"));
            docdata.getCanJiShi().add(item);
        }
        if (docdata.getJiaTingCFPFSSLB() == null) {
            JiaTingCFPFSSLB item = new JiaTingCFPFSSLB();
            item.setChuFangPFSSBZ(true);
            item.setChuFangPFSSLB(new CodeObject("1", "油烟机"));
            docdata.setJiaTingCFPFSSLB(item);
        }
        if (docdata.getJiaTingRLLXLB() == null) {
            JiaTingRLLXLB item = new JiaTingRLLXLB();
            item.setRanLiaoLX(new CodeObject("9", "其他"));
            docdata.setJiaTingRLLXLB(item);
        }
        if (docdata.getJiaTingYSLB() == null) {
            JiaTingYSLB item = new JiaTingYSLB();
            item.setYinShui(new CodeObject("2", "经净化过滤的水"));
            docdata.setJiaTingYSLB(item);
        }
        if (docdata.getJiaTingCSLB() == null) {
            JiaTingCSLB item = new JiaTingCSLB();
            item.setCeSuo(new CodeObject("1", "卫生厕所"));
            docdata.setJiaTingCSLB(item);
        }
        if (docdata.getJiaTingQCLLB() == null) {
            JiaTingQCLLB item = new JiaTingQCLLB();
            item.setQinChuLan(new CodeObject("1", "单设"));
            docdata.setJiaTingQCLLB(item);
        }
    }
}
